package com.wzp.yyptproject.controller;


import com.github.pagehelper.PageInfo;
import com.wzp.yyptproject.entity.PageEntity;
import com.wzp.yyptproject.entity.User;
import com.wzp.yyptproject.repository.UserRepository;
import com.wzp.yyptproject.request.UserRequest;
import com.wzp.yyptproject.request.UserRequestZhuCe;
import com.wzp.yyptproject.service.UserService;
import com.wzp.yyptproject.service.UserServiceimp;
import com.wzp.yyptproject.until.R;
import com.wzp.yyptproject.until.RedisUtils;
import com.wzp.yyptproject.until.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {
    @Autowired


    private UserService userService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/logincode")
    public String getCode() {
        String str = userService.getChickCode();
        return str;
    }

    @PostMapping("/pswdlogin")
    public String pswdLogin(@RequestBody User user) {
          String pswdlogin = userService.pswdLogin(user.getUname(), user.getUpassword(), user.getUidcard());
        if (pswdlogin.equals("success")) {
            return "success";
        }if (pswdlogin.equals("failed1")){
            return "failed1";
        }
        return "failed";
    }

    @PostMapping("/messlogin")
    public String login(@RequestBody UserRequest userRequest) {
        log.info("1111");
        Object code = redisUtils.get("checkCode");
     String messlogin = userService.MessLogin(userRequest.getUsername(), userRequest.getUphonenum(), userRequest.getUidcard(), userRequest.getCheckCode());
        if (messlogin.equals("success")) {
            return "success";
        }if (messlogin.equals("failed1")){
            return "failed1";
        }
        return "failed";
    }

    @GetMapping("/checkCode")
    public String checkCode(String ucode) {
        if (userService.findAndByUcode(ucode) == true) {
            return "激活成功";
        }
        return "激活失败";
    }


    @PostMapping("/adduser")
    public String addUser(@RequestBody UserRequestZhuCe userRequestZhuCe) {
        String code = UUIDUtils.getUUID() + UUIDUtils.getUUID();
        log.info(userRequestZhuCe.getUname());


        if (userService.addUser(userRequestZhuCe.getUname(), userRequestZhuCe.getUphonenum(), userRequestZhuCe.getUidcard(),
                userRequestZhuCe.getPhonecode(), code, userRequestZhuCe.getUpassword(), userRequestZhuCe.getUemail()).equals("ok")) {

            return "success";
        }
        if (userService.addUser(userRequestZhuCe.getUname(), userRequestZhuCe.getUphonenum(), userRequestZhuCe.getUidcard(),
                userRequestZhuCe.getPhonecode(), code, userRequestZhuCe.getUpassword(), userRequestZhuCe.getUemail()).equals("failed")) {

            return "failed1";
        }


        return "failed";


    }

    @GetMapping("/getemail")
    public String findAndByUemail(String uemail) {
        if (userService.findAndByUemail(uemail) == true) {
            return "success";
        } else {
            return "failed";
        }
    }

    @PostMapping("/updateuser")
    public String updateuser(@RequestBody UserRequestZhuCe userRequestZhuCe) {
        if (userService.updateuser(userRequestZhuCe.getUname(), userRequestZhuCe.getUphonenum(), userRequestZhuCe.getUidcard(), userRequestZhuCe.getPhonecode(),
                userRequestZhuCe.getUpassword()) == true) {

            return "success";
        }
        return "failed";
    }

    @GetMapping("/getuid")
    public User findAndByUUid(Integer uid) {
        return userService.findAndByUUid(uid);
    }

    @PostMapping("/getuser")
    public User findAndByUnameAndUidCard(@RequestBody User user) {
        return userService.findAndByUnameAndUidcard(user.getUname(), user.getUidcard());
    }


    /***********************************************************8*/
//    @GetMapping("/usercount")
//     public Integer Usercount(){
//    return userService.usercount();
//}
    @PostMapping("/usercount")
    public List<User> Usercount(){
        return userService.findAll();
    }

    @PostMapping(value = "/getList")
    public String getlist(int pageNum,int pageSize){
        Object users=userService.getUser(pageNum,pageSize);
        return com.alibaba.fastjson.JSON.toJSONString(users);
    }
//    @ResponseBody
    @GetMapping("/findByPage")
    public Page<User> findByPage(@RequestParam(value = "pageCode" ,defaultValue = "0")int pageCode,
                               @RequestParam(value = "pageSize" ,defaultValue = "4")int pageSize) {
        log.info("页码"+pageCode);
        log.info("页数"+pageSize);
          Page<User> userPage= userService.findByPage(pageCode,pageSize);
           System.out.println("数据"+userPage);
          return userPage;

//        return userService.findByPage(pageEntity.getPageCode(),pageEntity.getPageSize());
    }
@PostMapping("/updateusermess")
    public String updateUser(@RequestBody User user){
    System.out.println(user.toString());
        if (userService.updateUsermess(user)==true){
            return "success";
        }
        return "flied";
    }


    @PostMapping("/updatepswd")
    public String updatePswd(@RequestBody UserRequestZhuCe userRequestZhuCe){
        System.out.println(userRequestZhuCe.getNewupassword());
        System.out.println(userRequestZhuCe.getUname());
        System.out.println(userRequestZhuCe.getUidcard());
        System.out.println(userRequestZhuCe.getUpassword());

        if (userService.updatePswd(userRequestZhuCe.getUname(),userRequestZhuCe.getUidcard(),userRequestZhuCe.getUpassword(),userRequestZhuCe.getNewupassword())==true){
            return "success";
        }
        return "flied";
    }
}

    


